package cz.cvut.fit.bugtrac.controller;

import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cz.cvut.fit.bugtrac.dao.AuthorDAO;
import cz.cvut.fit.bugtrac.exception.BugTracException;
import cz.cvut.fit.bugtrac.manager.DataCollectionManager;
import cz.cvut.fit.bugtrac.manager.MetadataManager;
import cz.cvut.fit.bugtrac.persistence.Author;
import cz.cvut.fit.bugtrac.publicmodel.dto.metadata.AuthorDTO;
import cz.cvut.fit.bugtrac.util.DozerUtil;

/**
 * 
 * @author Jaroslav Crhonek
 * 
 *         Controller for testing and experimenting
 * 
 */
@Controller
@RequestMapping(value = "/my")
public class HelloWorldController {

	private static transient final Logger log = Logger
			.getLogger(HelloWorldController.class.getName());

	private AuthorDAO authorDAO;
	private DozerUtil dozerUtil;
	private MetadataManager metadataManager;
	private DataCollectionManager dataCollectionManager;

	/**
	 * @param metadataManager
	 *            the metadataManager to set
	 */
	@Autowired
	public void setMetadataManager(MetadataManager metadataManager) {
		this.metadataManager = metadataManager;
	}

	/**
	 * @param dozerUtil
	 *            the dozerUtil to set
	 */
	@Autowired
	public void setDozerUtil(DozerUtil dozerUtil) {
		this.dozerUtil = dozerUtil;
	}

	/**
	 * @param authorDAO
	 *            the authorDAO to set
	 */
	@Autowired
	public void setAuthorDAO(AuthorDAO authorDAO) {
		this.authorDAO = authorDAO;
	}

	@RequestMapping(value = { "/home" }, method = RequestMethod.GET, produces = "text/html")
	public String HelloWorld() {
		return "hello";
	}

	@RequestMapping(value = "/test", method = RequestMethod.GET, produces = "text/html")
	@ResponseBody
	public String ahoj() {
		return "HELLO WORLD";
	}

	@RequestMapping(value = "/testJPA", method = RequestMethod.GET, produces = "text/html")
	@ResponseBody
	public String testJPA() {
		if (authorDAO.getAuthors().size() != 2) {
			Author a = new Author();
			a.setCity("brno");
			a.setName("crhonjar");
			authorDAO.save(a);
			Author b = new Author();
			b.setCity("praha");
			b.setName("czebrpet");
			authorDAO.save(b);
			return "OK - entities created!";
		}
		return "OK...";
	}

	@RequestMapping(value = "/authors", method = RequestMethod.GET, produces = "text/html")
	@ResponseBody
	public String authors() {
		try {
			return dozerUtil.mapList(authorDAO.getAuthors(), AuthorDTO.class)
					.toString();
		} catch (BugTracException e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/reinitialize", method = RequestMethod.GET, produces = "text/html")
	@ResponseBody
	public String reinitialize() throws BugTracException {
		metadataManager.reinitialize();
		return "metaManager has been reinitialized";
	}

	@RequestMapping(value = "/collect")
	@ResponseBody
	public String collect() {
		try {
			dataCollectionManager.collectData();
			metadataManager.reinitialize();
		} catch (BugTracException e) {
			return e.getMessage();
		}
		return "data has been collected";
	}

	/**
	 * @return the dataCollectionManager
	 */
	public DataCollectionManager getDataCollectionManager() {
		return dataCollectionManager;
	}

	/**
	 * @param dataCollectionManager
	 *            the dataCollectionManager to set
	 */
	@Autowired
	public void setDataCollectionManager(
			DataCollectionManager dataCollectionManager) {
		this.dataCollectionManager = dataCollectionManager;
	}

}